package com.sds.sweng.swat.miplatform.ui.design.models.area.sub;

import java.util.List;

import org.codehaus.jackson.annotate.JsonIgnore;

import com.sds.sweng.swat.miplatform.ui.design.models.component.MButton;
import com.sds.sweng.swat.miplatform.ui.design.utils.CloneUtils;


public class SubButtonSubArea extends SubArea {

	public static final int
		DEFAULT_WIDTH = 300,
		MINIMUM_WIDTH = 10,
		GAP = 3;

	public static final String
		BUTTONS = "buttons";

	public SubButtonSubArea() {

	}

	public SubButtonSubArea clone() {
		SubButtonSubArea clone = (SubButtonSubArea) super.clone();
		clone.setButtons(CloneUtils.clone(getButtons()));

		return clone;
	}

	@JsonIgnore
	public List getButtons() {
		return getChildren();
	}

	@JsonIgnore
	public void setButtons(List<MButton> buttons) {
		removeChildren();

		for (int i=0;i<buttons.size();i++) {
			addChild(buttons.get(i));
		}

		firePropertyChange(BUTTONS, null, buttons);
	}

	public Object getPropertyValue(Object id) {
		if (BUTTONS.equals(id))
			return getButtons();

		return null;
	}

	public void setPropertyValue(Object id, Object value) {
		if (BUTTONS.equals(id))
			setButtons((List<MButton>) value);
	}

	@Override
	public boolean isEmpty() {
		if (getButtons().size() > 0)
			return false;

		return true;
	}

	@Override
	public int getRemainWidth() {
		int remainWidth = getWidth();

		for (int i=0;i<getButtons().size();i++) {
			MButton button = (MButton) getButtons().get(i);
			remainWidth -= button.getWidth();

			if (i < getButtons().size()-1)
				remainWidth -= GAP;
		}

		return remainWidth - MINIMUM_WIDTH;
	}

	public int getDefaultWidth() {
		return DEFAULT_WIDTH;
	}

	@Override
	public int getRequireWidth() {
		int requireWidth = 0;

		for (int i=0;i<getButtons().size();i++) {
			MButton button = (MButton) getButtons().get(i);
			requireWidth += button.getWidth();

			if (i < getButtons().size()-1)
				requireWidth += GAP;
		}

		return requireWidth;
	}

}
